<?php
App::Import('model', 'EvaluationPhysiographicUnit');
App::Import('model', 'EvaluationAnnex');
echo $this->Session->flash();
?>
<h1>AVALUOS DEL PREDIO <?php if (!empty($avaluos[0]['Property']['codigo'])) echo $avaluos[0]['Property']['codigo'] ?></h1>
<div id="loading" style="display: none;">
    <?php echo $html->image('loading.gif', array('border' => "0", 'align' => 'center')); ?>
</div>

<?php foreach ($avaluos as $avaluo): ?>
    <table id="tabla">
        <thead>
            <tr> 
                <th>Datos del avalúo</th>
                <th>Concepto</th>
                <th colspan="1"></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $calificacion = "Sin registro";
            switch ($avaluo['EvaluationAppraisal']['calificacion']) {
                case 1:
                    $calificacion = "Cumple";
                    break;
                case 2:
                    $calificacion = "Subsanable";
                    break;
                case 1:
                    $calificacion = "No cumple";
                    break;
                case 0:
                    $calificacion = "Sin registro";
                    break;
            }
            ?>
            <tr>
                <td>
                    <table>
                        <tr><td>Nombre del predio:</td><td><?php echo $avaluo['EvaluationAppraisal']['nombre_predio'] ?></td> </tr>
                        <tr><td>Departamento:</td><td><?php echo $avaluo['Departament']['name'] ?></td> </tr>
                        <tr><td>Vereda:</td><td><?php echo $avaluo['EvaluationAppraisal']['vereda'] ?></td> </tr>
                        <tr><td>Municipio:</td><td><?php echo $avaluo['City']['name'] ?></td> </tr>
                        <tr><td>Matrícula:</td><td><?php echo $avaluo['EvaluationAppraisal']['matricula'] ?></td> </tr>
                        <tr><td>Número catastral:</td><td><?php echo $avaluo['EvaluationAppraisal']['numero_catastral'] ?></td> </tr>
                        <tr>
                            <td><br></td>
                            <td><br></td>
                        </tr>
                        <?php
                        $unidades = new EvaluationPhysiographicUnit();
                        $areaTotalUnidades = $unidades->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id']));
                        $valorTotalUnidades = $unidades->field('Sum(EvaluationPhysiographicUnit.area * EvaluationPhysiographicUnit.valor_unitario)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id']));
                        $anexos = new EvaluationAnnex();
                        $valorTotalAnexos = $anexos->field('Sum(EvaluationAnnex.magnitud * EvaluationAnnex.valor_unitario)', array('EvaluationAnnex.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id']));
                        ?>
                        <tr><td <?php echo ((( round($avaluo['EvaluationAppraisal']['area'], 4) != round($areaTotalUnidades, 4)) or empty($areaTotalUnidades)) ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>><b>Área total avalúo (ha):</b></td><td <?php echo ((( round($avaluo['EvaluationAppraisal']['area'], 4) != round($areaTotalUnidades, 4)) or empty($areaTotalUnidades)) ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>><?php echo '<b>' . number_format($avaluo['EvaluationAppraisal']['area'], 4, ',', '.') . '</b>' ?></td> </tr>
                        <tr><td <?php echo ((( round($avaluo['EvaluationAppraisal']['area'], 4) != round($areaTotalUnidades, 4)) or empty($areaTotalUnidades)) ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?> ><b>Área total unidades (ha):</b></td><td <?php echo ((( round($avaluo['EvaluationAppraisal']['area'], 4) != round($areaTotalUnidades, 4)) or empty($areaTotalUnidades)) ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>><?php echo '<b>' . number_format($areaTotalUnidades, 4, ',', '.') . '</b>'; ?></td> </tr>
                        <tr>
                            <td><br></td>
                            <td><br></td>
                        </tr>
                        <?php
                        if ((round($valorTotalUnidades + $valorTotalAnexos) != round($avaluo['EvaluationAppraisal']['valor']) or empty($valorTotalUnidades))) {
                            $error = true;
                        } else {
                            $error = false;
                        }
                        ?>
                        <tr><td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><b>Valor total anexos ($):</b></td>
                            <td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><?php echo '<b>' . number_format($valorTotalAnexos, 0, ',', '.') . '</b>'; ?></td> </tr>
                        <tr><td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><b>Valor total unidades fisiográficas ($):</b></td>
                            <td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><?php echo '<b>' . number_format($valorTotalUnidades, 0, ',', '.') . '</b>'; ?></td> </tr>
                        <tr><td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><b>Unidades fisiográficas + anexos ($):</b></td>
                            <td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><?php echo '<b>' . number_format(($valorTotalAnexos + $valorTotalUnidades), 0, ',', '.') . '</b>'; ?></td> </tr>
                        <tr><td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><b>Valor inicial del avalúo ($):</b></td>
                            <td <?php echo $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'" ?>><?php echo '<b>' . number_format($avaluo['EvaluationAppraisal']['valor'], 0, ',', '.') . '</b>' ?></td> </tr>
                    </table>
                </td>
                <td>
                    <table>
                        <tr><td>Calificación:</td><td><?php echo $calificacion ?></td> </tr>
                        <tr><td>Concepto:</td><td><?php echo $avaluo['EvaluationAppraisal']['concepto'] ?></td> </tr>
                    </table>
                </td>
                <td>
                    <?php echo $ajax->link('Unidades', array('controller' => 'EvaluationPhysiographicUnits', 'action' => 'index', $avaluo['EvaluationAppraisal']['id']), array('class' => 'actions', 'update' => 'content', 'complete' => 'cargar()', 'indicator' => 'loading', 'escape' => false)); ?>
                    <br><br> 
                    <?php echo $ajax->link('Anexos', array('controller' => 'EvaluationAnnexes', 'action' => 'index', $avaluo['EvaluationAppraisal']['id']), array('class' => 'actions', 'update' => 'content', 'complete' => 'cargar()', 'indicator' => 'loading', 'escape' => false)); ?>
                    <br><br>
                    <?php echo $ajax->link('Editar', array('controller' => 'EvaluationAppraisals', 'action' => 'edit', $avaluo['EvaluationAppraisal']['id']), array('class' => 'actions', 'update' => 'content', 'complete' => 'cargar()', 'indicator' => 'loading', 'escape' => false)); ?>
                </td>
            </tr>
        </tbody>
    </table>
    <br>
    <br>
    <br>
<?php endforeach; ?>

<br>

<table width="100%" border="0"  CellSpacing=10  align="center" >

    <tbody>
        <tr>
            <td><?php echo $ajax->link('Adicionar', array('controller' => 'EvaluationAppraisals', 'action' => 'add', $property_id), array('class' => 'actions', 'update' => 'content', 'complete' => 'cargar()', 'indicator' => 'loading', 'escape' => false)); ?></td>
            <td><?php echo $ajax->link($html->image('regresar.gif', array('width' => '30', 'heigth' => '30', 'alt' => 'Regresar', 'title' => 'regresar')), array('controller' => 'properties', 'action' => 'index_appraisal'), array('update' => 'content', 'complete' => 'cargar()', 'indicator' => 'loading', 'escape' => false)); ?></td>
        </tr>
    </tbody>
</table>